package com.aiyige.utils;

import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;

/**
 * Created by zhuyakun on 2017/11/9.
 */

public class KeyboardUtil {
    public static void showKeyboard(Context context, View view) {
        view.requestFocus();
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(view, 0);
    }

    public static void hideKeyboard(Context context, View view) {
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
    }

    public static class KeyboardMonitor {
        Callback callback;
        View container;
        ViewTreeObserver viewTreeObserver;
        ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener;

        private KeyboardMonitor(Builder builder) {
            setCallback(builder.callback);
            setContainer(builder.container);
            setViewTreeObserver(builder.viewTreeObserver);
        }

        public static Builder newBuilder() {
            return new Builder();
        }

        public Callback getCallback() {
            return callback;
        }

        public void setCallback(Callback callback) {
            this.callback = callback;
        }

        public View getContainer() {
            return container;
        }

        public void setContainer(View container) {
            this.container = container;
        }

        public ViewTreeObserver getViewTreeObserver() {
            return viewTreeObserver;
        }

        public void setViewTreeObserver(ViewTreeObserver viewTreeObserver) {
            this.viewTreeObserver = viewTreeObserver;
        }

        public KeyboardMonitor install() {
            globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    int heightDiff = container.getRootView().getHeight() - container.getHeight();
                    if (heightDiff > UIHelper.dip2px(200)) {// if more than 200 dp, it's probably a keyboard...
                        callback.onKeyboardShow();
                    } else {
                        callback.onKeyboardHide();
                    }
                }
            };
            viewTreeObserver.addOnGlobalLayoutListener(globalLayoutListener);
            return this;
        }

        public void uninstall() {
            try {
                if (Build.VERSION.SDK_INT < 16) {
                    viewTreeObserver.removeGlobalOnLayoutListener(globalLayoutListener);
                } else {
                    viewTreeObserver.removeOnGlobalLayoutListener(globalLayoutListener);
                }
            } catch (Exception e) {

            }
        }

        public interface Callback {
            void onKeyboardShow();

            void onKeyboardHide();
        }

        public static final class Builder {
            private Callback callback;
            private View container;
            private ViewTreeObserver viewTreeObserver;

            private Builder() {
            }

            public Builder callback(Callback callback) {
                this.callback = callback;
                return this;
            }

            public Builder container(View container) {
                this.container = container;
                viewTreeObserver = container.getViewTreeObserver();
                return this;
            }

            public KeyboardMonitor build() {
                return new KeyboardMonitor(this);
            }

        }
    }

}
